<?php

/**
 * DictModel.php 注释
 * 
 * @package 
 * @subpackage 
 * @author rendong01 rendong237@126.com
 * 
 * : tool.php 2 2011-11-29 06:51:50Z rendong237@gmail.com $
 */
class DictModel{
	/**
	 * 数据库对象
	 *
	 * @var Mysql
	 */
	private $db;
	
	/**
	 * 构造方法
	 *
	 * @param Mysql $db
	 */
	public function __construct($db)
	{
		$this->db = $db;
	}
	
	/**
	 * 添加
	 */
	public function add( array $value )
	{
		$v = array();
//		$v["did"] = (int)$value["did"];
		$v["type"] = (int)$value["type"];
		$v["key"] = $value["key"];
		$v["enkey"] = $value["enkey"];
		$v["data"] = $value["data"];
		$v["endata"] = $value["endata"];
		$v["ismust"] = (int)$value["ismust"];
		$v["isoneline"] = (int)$value["isoneline"];
		$v["seq"] = (int)$value["seq"];
		$v["pubtime"] = date("Y-m-d H:i:s");
		
		$this->db->insert("dict", $v);
	}
	
	/**
	 * 修改
	 */
	public function change($did, array $value)
	{
		$v = array();
		$v["pubtime"] = date("Y-m-d H:i:s");
		isset($value["type"]) && $v["type"] = (int)$value["type"];
		isset($value["key"]) && $v["key"] = $value["key"];
		isset($value["enkey"]) && $v["enkey"] = $value["enkey"];
		isset($value["data"]) && $v["data"] = $value["data"];
		isset($value["endata"]) && $v["endata"] = $value["endata"];
		$v["ismust"] = (int)$value["ismust"];
		$v["isoneline"] = (int)$value["isoneline"];
		isset($value["issys"]) && $v["issys"] = (int)$value["issys"];
		isset($value["seq"]) && $v["seq"] = (int)$value["seq"];
		isset($value["pubtime"]) && $v["pubtime"] = $value["pubtime"];
		isset($value["isdel"]) && $v["isdel"] = (int)$value["isdel"];

		$this->db->update("dict", $v, 'did=' . (int)$did);
	}
	
	/**
	 * 删除
	 */
	public function del($did)
	{
		$v = array();
		$v['isdel'] = 1;
		$this->db->update("dict", $v, 'did=' . (int)$did);
		// $this->db->delete("dict", 'did=' .  (int)$did);
	}
	
	/**
	 * 读取列表
	 *
	 * @param Integer $nowp
	 * @param Integer $per_page
	 */
	public function fetchAll($nowp, $per_page)
	{
		$where = ' where isdel=0';		
		$totalNum = $this->db->fetchOne ('select count(*) from dict ' . $where);
        
        require_once APP . '/application/models/Util.php';
        
        $pageInfo = Util::page ($totalNum, $per_page, $nowp);
        $start    = $pageInfo ['start'];
        $list     = $this->db->fetchAll ('select * from dict' . $where .' order by seq desc, did asc limit ' . $start . ',' . $per_page);
        
        return Array ('page' => $pageInfo['page'], 'list' => $list);
	}
	
	/**
	 * 显示
	 */
	public function show($did)
	{
		return $this->db->fetchRow("select * from `dict` where did=\"?\"", array($did));
	}
}